// osgMeasurePtEvent.cpp : ���� DLL Ӧ�ó����ĵ���������
//

#include "osgMeasurePtEvent.h"
#include <osg/Group>
#include "Windows.h"
#include "IUserEventCallBack.h"

COsgMeasurePtEvent::COsgMeasurePtEvent(osgViewer::View *view, osg::Group *root, IEventCall *icall)
    : CBaseUserEvent(view),
      mView(view),
      mRoot(root),
      miCall(icall),
      isActivated(false)
{
    mEditSceneGroup = mRoot->getChild(1)->asGroup();
}

bool COsgMeasurePtEvent::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    if (isActivated)
    {
        //左键!
        if(IsLeftClick(ea) && GetKeyState(VK_CONTROL) < 0)
        {
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
            osg::Vec3d worldPt = pickResult.worldPt;

			if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
			{
				return false;
			}

            //发送消息 !
            char strMsg[99];
            sprintf_s(strMsg, " XYZ = %lf  %lf %lf", worldPt.x(), worldPt.y(), worldPt.z());
            miCall->notifyMeasureMsg(strMsg);

            //IEventCall->EmitSigShowOpsHint("按Backspace回退上一个点，按Z键撤销");
		}
		else if(IsKeyPress(ea))
		{
			if (ea.getKey() == osgGA::GUIEventAdapter::KEY_BackSpace || ea.getKey() == osgGA::GUIEventAdapter::KEY_Z)
			{
                //消息置空
                char strMsg[99] = "";
                miCall->notifyMeasureMsg(strMsg);
                //miCall->EmitSigShowOpsHint("按住ctrl键，点击鼠标左键测点，按ESC退出事件);
			}
		}
	}
	else
	{
		vecWorldCoord.clear();
	}

	return false;
}

void COsgMeasurePtEvent::updateActivatedStatus(std::vector<OperatingState> vecOpState)
{
	bool flag = false; 

	if (isActivated == true)
	{
		flag = true;
	}

	isActivated = isActivate(vecOpState, OPS_MEASURE_POINT);

    //刚不激活!
    if (isActivated == false && flag == true)
	{
        //消息置空
//		char strMsg[99] = "";
//		IEventCall->EmitSigNotifyMeasureMsg(strMsg);
//		IEventCall->EmitSigCloseMeasureDlg();
//		IEventCall->EmitSigShowOpsHint("");
	}

    if (isActivated && flag == false)																//刚激活!
	{
		vecWorldCoord.clear();
//        IEventCall->EmitSigShowOpsHint("按住ctrl键，点击鼠标左键测点，按ESC退出事件");
	}
}
